<template>
    <div>
        <div class="topbar">
            <div class="topbaritem">
                <router-link to="/home" tag="span"> <span @click="change">关闭</span> </router-link>
                <p>登录</p>
                <router-link to="/register" tag="span">注册</router-link>
            </div>
            <div class="loginbox">
                <div class="logincontent">
                    <div class="logo"><img
                            src="https://static.sonkwo.com/frontend/assets/28c5c9d87ae8d6d29643a68115d74c6d.png" alt="">
                    </div>
                    <div class="phone">
                        <van-icon name="phone-o" />
                        <input placeholder="电话" v-model="phone" type="text">
                    </div>
                    <div class="password">
                        <van-icon name="lock" />
                        <input placeholder="密码" v-model="pas" type="password" ref="password">
                        <span @click="hidePas" v-show="pasShow"><van-icon name="eye-o" /></span>
                        <span @click="showPas" v-show="!pasShow"><van-icon name="closed-eye" /></span>
                    </div>
                    <div class="login">
                        <div class="loginbutton">
                            <button @click="login">登录</button>
                        </div>
                    </div>
                    <div class="phonelogin">手机号快捷登录</div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import { Toast } from 'vant'



export default {
    data() {
        return {
            pasShow: false,
            user: null,
            phone: null,
            pas: null
        }
    }, methods: {
        hidePas() {
            //显示隐藏密码
            this.pasShow = !this.pasShow
            this.$refs.password.type = 'password'

        },
        showPas() {
            //显示隐藏密码
            this.pasShow = !this.pasShow
            this.$refs.password.type = 'text'
        },
        change() {
            this.$store.commit('change', true)
        },
        login() {
            // 登陆的时候，将localStorage来获取user，再来对比，登录的账号密码是否一致
            this.user = JSON.parse(localStorage.getItem('user'));
            if (!this.user) {
                Toast('未存在该账户')
                return
            }
            if (this.pas != this.user.pas || this.phone != this.user.phone) {
                Toast('密码或者账号填写错误')
                return
            } else {
                Toast('登录成功')
                this.$router.push("/home");
                // 创建一个token，模仿登录成功
                window.localStorage.setItem("token", JSON.stringify({
                    "phone": this.phone,
                    "pas": this.pas
                }));
                this.change()
            }
        }

    }
}
</script>

<style lang="scss" scoped>
.loginbox {
    width: 100%;

    .phonelogin {
        color: #ff5722;
        font-size: 15px;
    }

    .login {
        padding: 50px 0 25px 0;

        .loginbutton {
            button {
                width: 300px;
                background-color: #ff5722;
                border: none;
                height: 50px;
                color: #fff;
            }

        }
    }

    .logincontent {
        margin-top: 100px;

        .phone {
            display: flex;
            align-items: center;

            input {
                border: 0px;
                margin-left: 10px;
                height: 30px;
                width: 250px;
                border-bottom: 1px solid #cccccc;
                margin-left: 20px;
            }
        }

        .password {
            display: flex;
            align-items: center;
            margin-top: 20px;
            position: relative;

            input {
                border: 0px;
                margin-left: 10px;
                height: 30px;
                width: 250px;
                border-bottom: 1px solid #cccccc;
                margin-left: 20px;
            }

            span {
                position: absolute;
                right: 0;
            }
        }

        .logo {
            margin-bottom: 20px;

            img {
                width: 166px;
                height: 64px;
            }
        }

        height: 100px;

        padding: 80px 20px;
        display: flex;
        justify-content: center;
        flex-direction: column;
        align-items: center;
    }
}

.topbar {
    height: 44px;

    .topbaritem {
        padding: 0 25px;
        height: 44px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #333333;

        span {
            color: #999999;
        }

        p {
            color: #cccccc;
            font-size: 15px;
        }
    }
}
</style>